]> git.r.bdr.sh - rbdr/super-polarity/blobdiff - Super Polarity/Actors/MainShip.cs
Implements polarity system
[rbdr/super-polarity] / Super Polarity / Actors / MainShip.cs
diff --git a/Super Polarity/Actors/MainShip.cs b/Super Polarity/Actors/MainShip.cs
new file mode 100644 (file)
index 0000000..851c658
--- /dev/null
@@ -0,0 +1,163 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Microsoft.Xna.Framework;
+using Microsoft.Xna.Framework.Content;
+using Microsoft.Xna.Framework.Graphics;
+
+namespace SuperPolarity
+{
+    class MainShip : Ship
+    {
+        
+        uint Multiplier;
+        uint Lives;
+        uint Score;
+        ParticleEngine particleEngine;
+
+        public MainShip(Game newGame) : base(newGame) {}
+
+        public override void Initialize(Texture2D texture, Vector2 position)
+        {
+            base.Initialize(texture, position);
+
+            InitParticleEngine();
+            SetPolarity(Polarity.Positive);
+
+            Multiplier = 1;
+            Lives = 3;
+            Score = 0;
+
+            BindInput();
+        }
+
+        void InitParticleEngine()
+        {
+            List<Texture2D> texturesList = new List<Texture2D>();
+            texturesList.Add(game.Content.Load<Texture2D>("Graphics\\circle"));
+            texturesList.Add(game.Content.Load<Texture2D>("Graphics\\diamond"));
+            texturesList.Add(game.Content.Load<Texture2D>("Graphics\\star"));
+
+            particleEngine = new ParticleEngine(texturesList, Position);
+        }
+
+        void BindInput()
+        {
+            InputController.Bind("moveX", HandleHorizontalMovement);
+            InputController.Bind("moveY", HandleVerticalMovement);
+            InputController.Bind("changePolarity", HandleChangePolarity);
+        }
+
+        protected void HandleChangePolarity(float value)
+        {
+            SwitchPolarity();
+        }
+
+        public void HandleHorizontalMovement(float value)
+        {
+            Acceleration.X = value * AccelerationRate;
+
+            if (value > 0.1 && Velocity.X < 0 || value < 0.1 && Velocity.X > 0)
+            {
+                Acceleration.X *= 2;
+            }
+
+            if (value > 0.1 && Velocity.Y < 0 || value < 0.1 && Velocity.Y > 0)
+            {
+                Acceleration.Y *= 2;
+            }
+        }
+
+        public void HandleVerticalMovement(float value)
+        {
+            Acceleration.Y = value * AccelerationRate;
+        }
+
+        public override void SwitchPolarity()
+        {
+            base.SwitchPolarity();
+            SwitchParticleEngine(CurrentPolarity);
+        }
+
+        public override void SetPolarity(Polarity newPolarity)
+        {
+            base.SetPolarity(newPolarity);
+            SwitchParticleEngine(newPolarity);
+        }
+
+        protected void SwitchParticleEngine(Polarity polarity)
+        {
+            if (polarity == Polarity.Positive)
+            {
+                particleEngine.Color = Color.Red;
+            }
+            else if (polarity == Polarity.Negative)
+            {
+                particleEngine.Color = Color.Blue;
+            }
+            else
+            {
+                particleEngine.Color = Color.Gray;
+            }
+        }
+
+        public override void Update(GameTime gameTime)
+        {
+            base.Update(gameTime);
+            particleEngine.EmitterLocation = Position;
+            particleEngine.Update();
+            ConstrainToEdges();
+        }
+
+        public override void Magnetize(Ship ship, float distance, float angle)
+        {
+        }
+
+        protected void ConstrainToEdges()
+        {
+            if (Position.X < 0)
+            { 
+                Position.X = 0;
+
+                if (Velocity.X < 0)
+                {
+                    Velocity.X = 0;
+                }
+            }
+            if (Position.X > game.GraphicsDevice.Viewport.Width)
+            { 
+                Position.X = game.GraphicsDevice.Viewport.Width;
+
+                if (Velocity.X > 0)
+                {
+                    Velocity.X = 0;
+                }
+            }
+            if (Position.Y < 0) 
+            {
+                Position.Y = 0;
+
+                if (Velocity.Y < 0)
+                {
+                    Velocity.Y = 0;
+                }
+            }
+            if (Position.Y > game.GraphicsDevice.Viewport.Height)
+            {
+                Position.Y = game.GraphicsDevice.Viewport.Height;
+
+                if (Velocity.Y < 0)
+                {
+                    Velocity.Y = 0;
+                }
+            }
+        }
+
+        public override void Draw(SpriteBatch spriteBatch)
+        {
+            particleEngine.Draw(spriteBatch);
+            base.Draw(spriteBatch);
+        }
+    }
+}